const Service = require("uni-cloud-router").Service;
module.exports = class UserService extends Service {
	async getUserInfo({
		_userID
	}) {
		let userDataInDB = await this.db.collection('userdata').where({
			_userID
		}).get();

		if (userDataInDB.affectedDocs !== 0 || userDataInDB.data.length !== 0) {
			return {
				status: 0,
				data: userDataInDB.data,
				msg: "获取成功"
			}
		} else {
			return {
				status: 1,
				msg: "请填写个人信息"
			}
		}

		return {
			status: -1,
			msg: "稍后重试"
		}
	}

	async setUserInfo({
		name,
		phone,
		address,
		detailAddre,
		idCard,
		bankID,
		wkID,
		userType,
		sex,
		_userID,
		password,
		workStatus,
		pm_company
	}) {
		let encodeIdCard = Buffer.from(idCard, 'utf8');
		let encodeBankID = Buffer.from(bankID, 'utf8');
		let encodePassword = Buffer.from(password, 'utf8');

		let userDataInDB = await this.db.collection('userdata').where({
			_userID
		}).get()

		// 已经存在修改
		if (userDataInDB.affectedDocs !== 0 || userDataInDB.data.length !== 0) {
			let editUserData = await this.db.collection('userdata').where({
				_userID
			}).update({
				name,
				phone,
				address,
				detailAddre,
				idCard: encodeIdCard.toString('hex'),
				bankID: encodeBankID.toString('hex'),
				wkID,
				userType,
				password: encodePassword.toString('hex'),
				sex
			})

			if (editUserData.affectedDocs !== 0) {
				return {
					status: 0,
					mag: "保存成功"
				}
			}
		} else {
			//添加
			let setUserData = await this.db.collection('userdata').add({
				name,
				phone,
				address,
				detailAddre,
				idCard: encodeIdCard.toString('hex'),
				bankID: encodeBankID.toString('hex'),
				wkID,
				userType,
				password: encodePassword.toString('hex'),
				_userID,
				sex,
				workStatus,
				pm_company
			});

			if (setUserData.affectedDocs !== 0) {
				return {
					status: 0,
					mag: "保存成功"
				}
			}
		}

		return {
			status: -1,
			msg: '稍后重试',
			event
		}
	}

	async getUserType({
		userID
	}) {
		let userTypeInDB = await this.db.collection('userdata').where({
			_userID: userID
		}).get()

		if (userTypeInDB.affectedDocs !== 0 || userTypeInDB.data.length !== 0) {
			return {
				status: 0,
				msg: "存在",
				userType: userTypeInDB.data[0].userType,
				wkID: userTypeInDB.data[0].wkID
			}
		} else {
			return {
				status: 1,
				msg: "没有添加个人信息"
			}
		}

		return {
			status: -1,
			msg: "稍后重试"
		}
	}

	async getUserInfoByWkID(wkID) {
		let userDataInDB = await this.db.collection('userdata').where({
			wkID
		}).get();

		if (userDataInDB.affectedDocs !== 0 || userDataInDB.data.length !== 0) {
			return {
				status: 0,
				data: userDataInDB.data,
				msg: "获取成功"
			}
		} else {
			return {
				status: 1,
				msg: "没有该用户"
			}
		}

		return {
			status: -1,
			msg: "稍后重试"
		}
	}
	
	async pmBindWorker(wkID,pmID,pm_company){
		const dbCmd = this.db.command;
		let saveWK = await this.db.collection('userdata').where({
			wkID
		}).update({
			pm_company
		})
		
		if(saveWK.affectedDocs !== 0){
			return {
				status:0,
				msg:"绑定成功"
			}
		} else {
			return {
				status:1,
				msg:"稍后重试"
			}
		}
		
		return {
			status:-1,
			msg:"稍后重试"
		}
	}
	
	async getWorkerIDs(userID){
		let res = await this.db.collection('userdata').where({
			_userID: userID
		}).field({"workers":true}).get()
		
		if(res.affectedDocs !== 0){
			return {
				status:0,
				msg:"成功",
				data:res.data
			}
		} else {
			return{
				status:1,
				msg:"稍后重试"
			}
		}
		
		return {
			status:-1,
			msg:"稍后重试"
		}
	}
	
	async relieveBindWorke(userID,wkID){
		const dbCmd = this.db.command;
		let res = await this.db.collection('userdata').where({
			wkID
		}).update({
			pm_company: ""
		})
		
		if(res.affectedDocs !== 0){
			return {
				status:0,
				msg:"解绑成功"
			}
		} else {
			return{
				status:1,
				msg:"稍后重试"
			}
		}
		
		return {
			status:-1,
			msg:"稍后重试"
		}
	}
	
	async getUserDataByPmCompany(pm_company){
		let res = await this.db.collection('userdata').where({
			pm_company
		}).get();
		if(res.affectedDocs !== 0){
			return {
				status:0,
				msg:"获取成功",
				data:res.data
			}
		} else {
			return{
				status:1,
				msg:"稍后重试"
			}
		}
		
		return {
			status:-1,
			msg:"稍后重试"
		}
	}
}
